﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class InvAdjustmentRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15351;
        protected GridView gvAdjustmentRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotal;
        protected Label lblTotalExtendedPrice;
        protected Label lblTotalExtendedPriceText;
        protected Label lblTotalGrossWgt;
        protected Label lblTotalGrossWgtText;
        protected Label lblTotalPrice;
        protected Label lblTotalPriceText;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalExtendedPrice;
        private decimal TotalGrossWgt;
        private decimal TotalPrice;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2gvInOutStockList(InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(invAdjustmentRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetAdjustMentRptDataSet(invAdjustmentRptQueryEntity);
            base.BindData2GridView(this.gvAdjustmentRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            invAdjustmentRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = invAdjustmentRptQueryEntity.CurrentPage;
            invAdjustmentRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetAdjustMentRptDataSet(invAdjustmentRptQueryEntity))
                {
                    base.Export2Excel("InvAdjustMentRpt.xls", "15351", this.gvAdjustmentRpt, ds);
                }
            }
            finally
            {
                invAdjustmentRptQueryEntity.IsGetAll = false;
                invAdjustmentRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetInvAdjustmentRptQueryEntity(invAdjustmentRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            invAdjustmentRptQueryEntity.CurrentPage = "0";
            this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            invAdjustmentRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            invAdjustmentRptQueryEntity.ADJUSTMENT_ID = "";
            invAdjustmentRptQueryEntity.ADJUSTMENT_DATE = "";
            invAdjustmentRptQueryEntity.OWNER_ID = "";
            invAdjustmentRptQueryEntity.SKU_ID = "";
            invAdjustmentRptQueryEntity.SKUCLASS_ID = "";
            invAdjustmentRptQueryEntity.DESCR = "";
            invAdjustmentRptQueryEntity.LOC = "";
            invAdjustmentRptQueryEntity.LOT = "";
            invAdjustmentRptQueryEntity.REASON_CODE = "";
            invAdjustmentRptQueryEntity.LOT_ATTR01 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR02 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR03 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR04 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR05 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR06 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR07 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR08 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR09 = "";
            invAdjustmentRptQueryEntity.LOT_ATTR10 = "";
            invAdjustmentRptQueryEntity.EXPIRY_DATE = "";
            invAdjustmentRptQueryEntity.PRODUCE_DATE = "";
            invAdjustmentRptQueryEntity.EXTERNAL_LOT = "";
            this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            if (int.Parse(invAdjustmentRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invAdjustmentRptQueryEntity.CurrentPage = (int.Parse(invAdjustmentRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            if (int.Parse(invAdjustmentRptQueryEntity.CurrentPage) > 0)
            {
                invAdjustmentRptQueryEntity.CurrentPage = (int.Parse(invAdjustmentRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
            this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
        }

        private InvAdjustmentRptQueryEntity GetInvAdjustmentRptQueryEntity()
        {
            return (base.PageQueryEntitySession as InvAdjustmentRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
                invAdjustmentRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
            }
        }

        protected void gvAdjustmentRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15351", this.gvAdjustmentRpt);
        }

        protected void gvAdjustmentRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblUnitPrice")) != null)
                {
                    this.TotalPrice += decimal.Parse((((Label) e.Row.FindControl("lblUnitPrice")).Text != "") ? ((Label) e.Row.FindControl("lblUnitPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblExtendedPrice")) != null)
                {
                    this.TotalExtendedPrice += decimal.Parse((((Label) e.Row.FindControl("lblExtendedPrice")).Text != "") ? ((Label) e.Row.FindControl("lblExtendedPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblGrossWgt")) != null)
                {
                    this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblGrossWgt")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalPriceText.Text = base.GetPreferenceDigit(this.TotalPrice);
                this.lblTotalExtendedPriceText.Text = base.GetPreferenceDigit(this.TotalExtendedPrice);
                this.lblTotalGrossWgtText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
            }
        }

        protected void gvAdjustmentRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = this.GetInvAdjustmentRptQueryEntity();
                invAdjustmentRptQueryEntity.SortField = e.SortExpression;
                if (invAdjustmentRptQueryEntity.SortDirection == "ASC")
                {
                    invAdjustmentRptQueryEntity.SortDirection = "DESC";
                }
                else if (invAdjustmentRptQueryEntity.SortDirection == "DESC")
                {
                    invAdjustmentRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity = new InvAdjustmentRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invAdjustmentRptQueryEntity.ADJUSTMENT_ID = this.PreviousPage.AdjustMentID;
                    invAdjustmentRptQueryEntity.ADJUSTMENT_DATE = this.PreviousPage.AdjustMentDate;
                    invAdjustmentRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    invAdjustmentRptQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    invAdjustmentRptQueryEntity.SKUCLASS_ID = this.PreviousPage.SkuClassID;
                    invAdjustmentRptQueryEntity.DESCR = this.PreviousPage.SkuDescr;
                    invAdjustmentRptQueryEntity.LOC = this.PreviousPage.Loc;
                    invAdjustmentRptQueryEntity.LOT = this.PreviousPage.Lot;
                    invAdjustmentRptQueryEntity.REASON_CODE = this.PreviousPage.ReasonCode;
                    invAdjustmentRptQueryEntity.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    invAdjustmentRptQueryEntity.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    invAdjustmentRptQueryEntity.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    invAdjustmentRptQueryEntity.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    invAdjustmentRptQueryEntity.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    invAdjustmentRptQueryEntity.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    invAdjustmentRptQueryEntity.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    invAdjustmentRptQueryEntity.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    invAdjustmentRptQueryEntity.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    invAdjustmentRptQueryEntity.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    invAdjustmentRptQueryEntity.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    invAdjustmentRptQueryEntity.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    invAdjustmentRptQueryEntity.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                }
                else
                {
                    invAdjustmentRptQueryEntity.ADJUSTMENT_ID = "";
                    invAdjustmentRptQueryEntity.ADJUSTMENT_DATE = "";
                    invAdjustmentRptQueryEntity.OWNER_ID = "";
                    invAdjustmentRptQueryEntity.SKU_ID = "";
                    invAdjustmentRptQueryEntity.SKUCLASS_ID = "";
                    invAdjustmentRptQueryEntity.DESCR = "";
                    invAdjustmentRptQueryEntity.LOC = "";
                    invAdjustmentRptQueryEntity.LOT = "";
                    invAdjustmentRptQueryEntity.REASON_CODE = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR01 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR02 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR03 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR04 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR05 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR06 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR07 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR08 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR09 = "";
                    invAdjustmentRptQueryEntity.LOT_ATTR10 = "";
                    invAdjustmentRptQueryEntity.EXPIRY_DATE = "";
                    invAdjustmentRptQueryEntity.PRODUCE_DATE = "";
                    invAdjustmentRptQueryEntity.EXTERNAL_LOT = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invAdjustmentRptQueryEntity.PageSize = this.gvAdjustmentRpt.PageSize.ToString();
            this.PagerFooter.PageSize = this.gvAdjustmentRpt.PageSize;
            invAdjustmentRptQueryEntity.IsGetAll = false;
            invAdjustmentRptQueryEntity.CurrentPage = "0";
            invAdjustmentRptQueryEntity.SortField = "SKU_ID";
            invAdjustmentRptQueryEntity.SortDirection = "DESC";
            this.SetInvAdjustmentRptQueryEntity(invAdjustmentRptQueryEntity);
            this.BindData2gvInOutStockList(invAdjustmentRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvAdjustmentRpt);
                this.LoadData();
            }
        }

        public void SetInvAdjustmentRptQueryEntity(InvAdjustmentRptQueryEntity invAdjustmentRptQueryEntity)
        {
            base.PageQueryEntitySession = invAdjustmentRptQueryEntity;
        }

        public InvAdjustmentRptQuery PreviousPage
        {
            get
            {
                return (InvAdjustmentRptQuery) base.PreviousPage;
            }
        }
    }
}

